package repositories

import (
	"context"
	"errors"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"nodeseller/datamodels"
	mymongo "nodeseller/mongo"
)

//User表的操作
type IProduct interface {
	//连接数据库
	Conn()(error)
	//插入数据库
	Insert(user *datamodels.Product)(interface{},error)
	//删除数据库
	Delete(primitive.ObjectID)bool
	//查看产品
	SelectByKey(primitive.ObjectID)(*datamodels.Product,error)
	//查看正在进行中的产品
	SelectAll(string)([]datamodels.Product,error)
	//查询已经完成订单
	OrderFined(phonenumber string)(result []datamodels.Product,err error)
	//查询已经下架订单
	FindDelete(phonenumber string)(result []datamodels.Product,err error)
}
type ProductManager struct {
	mongoDataColletion mymongo.Collection
}

func NewProductManager()IProduct  {
	mongodb,err := mymongo.Get()
	if err != nil{
		return nil
	}
	return &ProductManager{
		mongodb.Collection("product"),
	}
}
//连接数据库
func (p *ProductManager)Conn()(error)  {
	if p.mongoDataColletion == nil{
		mongodb,err := mymongo.Get()
		if err != nil{
			return err
		}
		p.mongoDataColletion = mongodb.Collection("product")
	}
	return nil
}
//创建产品
func (p *ProductManager)Insert(user *datamodels.Product)(interface{},error)  {
	//判断连接是否存在
	if err := p.Conn();err != nil{
		return nil, err
	}
	insertresult,err := p.mongoDataColletion.InsertOne(context.TODO(),*user)
	if err != nil{
		return nil, err
	}
	if insertresult.InsertedID != nil{
		return insertresult.InsertedID,nil
	}
	return nil,nil
}
//删除产品
func (p *ProductManager)Delete(prouductid primitive.ObjectID)bool  {
	//判断连接是否存在
	if err := p.Conn();err != nil{
		return false
	}
	//删除
	deleteresult,err := p.mongoDataColletion.UpdateOne(context.TODO(),bson.M{"_id":prouductid},bson.D{{"$set",bson.M{"delete":true}}})
	if err != nil{
		return false
	}
	if deleteresult.ModifiedCount > 0{
		return true
	}
	return false
}
//查看产品
func (p *ProductManager)SelectByKey(prouductid primitive.ObjectID)(result *datamodels.Product,err error)  {
	//判断连接是否存在
	if err := p.Conn();err != nil{
		return nil,err
	}
	p.mongoDataColletion.FindOne(context.TODO(),bson.M{"_id":prouductid}).Decode(&result)
	return result,nil
}
//查看正在进行中的产品
func (p *ProductManager)SelectAll(phonenumber string)(result []datamodels.Product,err error)  {
	//判断连接是否存在
	if err := p.Conn();err != nil{
		return nil,err
	}
	cur,err :=  p.mongoDataColletion.Find(context.TODO(),bson.M{"phonenumber":phonenumber,"delete":false,"finedorder":false})
	if err != nil{
		return nil,errors.New("数据库连接错误")
	}
	for cur.Next(context.TODO()){
		var elem datamodels.Product
		err := cur.Decode(&elem)
		if err != nil{
			return nil, errors.New("数据库解码错误")
		}
		result = append(result,elem)
	}
	return result,nil
}
//查询已经完成的订单
func (p *ProductManager)OrderFined(phonenumber string)(result []datamodels.Product,err error)  {
	//判断连接是否存在
	if err := p.Conn();err != nil{
		return nil,err
	}
	//判断连接是否存在
	if err := p.Conn();err != nil{
		return nil,err
	}
	cur,err :=  p.mongoDataColletion.Find(context.TODO(),bson.M{"phonenumber":phonenumber,"delete":false,"finedorder":true})
	if err != nil{
		return nil,errors.New("数据库连接错误")
	}
	for cur.Next(context.TODO()){
		var elem datamodels.Product
		err := cur.Decode(&elem)
		if err != nil{
			return nil, errors.New("数据库解码错误")
		}
		result = append(result,elem)
	}
	return result,nil
}
//查询已经下架的订单
func (p *ProductManager)FindDelete(phonenumber string)(result []datamodels.Product,err error)  {
	//判断连接是否存在
	if err := p.Conn();err != nil{
		return nil,err
	}
	//判断连接是否存在
	if err := p.Conn();err != nil{
		return nil,err
	}
	cur,err :=  p.mongoDataColletion.Find(context.TODO(),bson.M{"phonenumber":phonenumber,"delete":true,"finedorder":false})
	if err != nil{
		return nil,errors.New("数据库连接错误")
	}
	for cur.Next(context.TODO()){
		var elem datamodels.Product
		err := cur.Decode(&elem)
		if err != nil{
			return nil, errors.New("数据库解码错误")
		}
		result = append(result,elem)
	}
	return result,nil
}

